import pool from '../config/database';
export interface dictData {
  id?: number;
  dict_id?: number;
  dict_value: string;
  dict_label: string;
  remark: string;
  sort: number;
  create_by?: string;
  update_by?: string;
}
export class DictDataModel {
  async create(dictData: dictData): Promise<any> {
    const { dict_id, dict_value, dict_label, remark, sort, create_by, update_by } = dictData;
    const sql = `insert into sys_dict_data(dict_id, dict_value, dict_label, remark, sort, create_by, update_by) values(${dict_id}, '${dict_value}', '${dict_label}', '${remark}', ${sort}, '${create_by}', '${update_by}')`;
    const [result] = await pool.execute(sql)
    return result;
  }
  async delete(ids: number[], userId?: string): Promise<any> {
    let sql = `delete from sys_dict_data where id in (${ids.join(',')})`
    if (userId) {
      sql = `delete from sys_dict_data where id in (${ids.join(',')}) and create_by = '${userId}'`;
    }
    const [result] = await pool.execute(sql)
    return result;
  }
  async findByDictId(dictId: number): Promise<any> {
    const [result] = await pool.execute(`select * from sys_dict_data where dict_id = ${dictId}`)
    return result;
  }
  async update(dictData: dictData): Promise<any> {
    const { id, dict_value, dict_label, remark, sort, update_by } = dictData;
    const sql = `update sys_dict_data set dict_value = '${dict_value}', dict_label = '${dict_label}', remark = '${remark}', sort = ${sort}, update_by = '${update_by}' where id = ${id}`
    const [result] = await pool.execute(sql)
    return result;
  }
}